package csc.fresher.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import csc.fresher.dao.CustomerDao;
import csc.fresher.dao.RoleDao;
import csc.fresher.entity.Customer;
import csc.fresher.entity.Role;

public class RoleDaoImpl implements RoleDao {

	@Autowired
	DataSource dataSource;

	public Connection getConnection() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			return DriverManager.getConnection(
					"jdbc:mysql://20.203.182.23:3306/finalProject", "root",
					"root");
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	private static final Logger logger = Logger
			.getLogger(SavingAccountDaoImpl.class);

	@Override
	public Role getRoleByID(int roleID) {
		String query = "SELECT * FROM `Role` WHERE `RoleID`=?";
		Role role = new Role();
		Connection conn = null;
		PreparedStatement preparedStatement = null;
		try {

			conn = getConnection();
			preparedStatement = conn.prepareStatement(query);
			preparedStatement.setLong(1, roleID);

			ResultSet rs = preparedStatement.executeQuery();

			role = getRole(rs);
		} catch (SQLException e) {
			logger.error(e);
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				logger.error(e);
				e.printStackTrace();
			}
		}
		return role;
	}

	@Override
	public List<Role> getRoleByName(String roleName) {
		List<Role> resultList = new ArrayList<Role>();
		String query = "SELECT * FROM `Role` WHERE `RoleName` LIKE '%"
				+ roleName + "%'";
		Connection conn = null;
		PreparedStatement preparedStatement = null;
		try {

			conn = getConnection();
			preparedStatement = conn.prepareStatement(query);

			ResultSet rs = preparedStatement.executeQuery();

			while (rs.next()) {
				Role role = new Role();
				role = getRole(rs);
				resultList.add(role);
			}
		} catch (SQLException e) {
			logger.error(e);
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				logger.error(e);
				e.printStackTrace();
			}
		}
		return resultList;
	}

	public Role getRole(ResultSet rs) throws SQLException {
		Role role = new Role();
		if (rs.next()) {
			role.setRoleID(rs.getInt("RoleID"));
			role.setRoleName(rs.getString("RoleName"));
		}
		return role;
	}

	public static void main(String[] agrs) {
		RoleDao roleDao = new RoleDaoImpl();
		List<Role> resultList = roleDao.getRoleByName("S");

		for (int i = 0; i < resultList.size(); i++) {
			Role role = new Role();
			role = resultList.get(i);
			System.out.println(role.getRoleName());
		}
	}
}
